﻿using Newtonsoft.Json;

namespace Ecobee.Net
{
	/// <summary>
	/// The function object is defined by its functionType and one or more additional properties. The property
	/// list is variable depending on the type of function. Functions are used to perform more complex operations 
	/// on a thermostat or user which are too complex with simple property modifications. Functions are used to 
	/// modify read-only objects where appropriate.
	/// </summary>
	public class Function : ApiObject
	{
		private string _type = string.Empty;
		private ApiObject _parameters = null;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="parameters"></param>
		public Function(string type, ApiObject parameters)
		{
			this.Type = type;
			this.Parameters = parameters;
		}

		/// <summary>
		/// The function type name. See the type name in the function documentation. 
		/// </summary>
		[JsonProperty(PropertyName = "type", Order = 1)]
		public string Type
		{
			get
			{
				return _type;
			}
			set
			{
				_type = value;
			}
		}

		/// <summary>
		/// A map of key=value pairs as the parameters to the function. See individual function documentation for the properties. 
		/// </summary>
		[JsonProperty(PropertyName = "params", Order = 2, DefaultValueHandling = DefaultValueHandling.Ignore)]
		public ApiObject Parameters
		{
			get
			{
				return _parameters;
			}
			set
			{
				_parameters = value;
			}
		}

		public static class FunctionNames
		{
			public static string Acknowledge = "acknowledge";
			public static string ControlPlug = "controlPlug";
			public static string CreateVacation = "createVacation";
			public static string DeleteVacation = "deleteVacation";
			public static string ResumeProgram = "resumeProgram";
			public static string SendMessage = "sendMessage";
			public static string SetHold = "setHold";
		}

		// ***
		// *** Create Pre-defined functions
		// ***
		public static Function Acknowledge(Acknowledge acknowledge)
		{
			return new Function(FunctionNames.Acknowledge, acknowledge);
		}

		public static Function ControlPlug = new Function(FunctionNames.ControlPlug, null);
		
		public static Function CreateVacation = new Function(FunctionNames.CreateVacation, null);

		public static Function DeleteVacation(Vacation vacation)
		{
			return new Function(FunctionNames.DeleteVacation, vacation);
		}

		public static Function ResumeProgram()
		{
			return new Function(FunctionNames.ResumeProgram, null);
		}

		public static Function SendMessage(Message sendMessage)
		{
			return new Function(FunctionNames.SendMessage, sendMessage);
		}

		public static Function SetHold = new Function(FunctionNames.SetHold, null);
	}
}
